//
//  AEPointsMallViewController.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/13.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEPointsMallViewController.h"
#import "AEPointsMallViewModel.h"
#import "AEPointsMallTopView.h"
#import "AEPointsMallViewCell.h"
#import "AEMyExchangeRootViewController.h" // 我的兑换
//#import "AEMyExchangeViewController.h"
#import "AEGoodsDetailViewController.h" // 商品详情
#import "AEHistoryPointsViewController.h" // 历史积分
#import "AEPointsAcquireViewController.h" // 积分获取
#import "AE_Solar-Swift.h"

@interface AEPointsMallViewController ()
@property (nonatomic, strong) AEPointsMallViewModel *viewModel;

@property (nonatomic, strong) AEPointsMallTopView *topView;

@end

@implementation AEPointsMallViewController

- (void)dealloc {
    debugMethod();
    
}

- (void)initWithVar {
    [super initWithVar];
    
    @weakify(self);
    [[AEAppDelegateTool sharedInstance] requestWithUserInfo:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            self.viewModel.userInfoModel = respond.data;
            [self initWithTopView];
        }
    }];
}

- (void)initWithViewModel {
    _viewModel = [AEPointsMallViewModel new];
    
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    self.navigationItem.title = self.viewModel.title;
    
    /*
    if (@available(iOS 15.0, *))
    {
        UINavigationBarAppearance *nav = [UINavigationBarAppearance new];
        [nav configureWithOpaqueBackground]; // 重置背景和阴影颜色
        // 背景色
        nav.backgroundColor = kWhiteColor;
        
        nav.shadowImage = [[UIImage alloc] init];
        
        // 去掉半透明效果
        nav.backgroundEffect = nil;
        
        // 去掉导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
        nav.shadowColor = [UIColor clearColor];
        
//        nav.titleTextAttributes = @{NSForegroundColorAttributeName:navInfo.titleColor, NSFontAttributeName:navInfo.titleFont};
        self.navigationController.navigationBar.scrollEdgeAppearance = nav; // 带scroll滑动的页面
        self.navigationController.navigationBar.standardAppearance = nav; // 常规页面
        
        return;
    }
    
    // 导航栏背景
    [self.navigationController.navigationBar setBarTintColor:kWhiteColor];
    
    // navBar title Color font
//    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:navInfo.titleColor, NSFontAttributeName:navInfo.titleFont}];
    
    // 去除自带的顶部阴影
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];

    // 设置导航栏控制器颜色
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:kWhiteColor] forBarMetrics:UIBarMetricsDefault];
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setTranslucent:)])
    {
        // 设置导航栏是否具有透明度这个功能
        // 在iOS7以后，默认为YES。{控件从（0，0）点开始，设置为NO,则从nav底部（0，64）开始}；
//        self.navigationBar.translucent = NO;
    }
     */
}




//override func navigationBarStyle() -> NavigationBarStyle {
//    return .baseColor
//}



- (void)initWithSubView {
    [super initWithSubView];
    
    self.view.backgroundColor = kWhiteColor;
    
    [self hiddenHeaderView:NO];
    [self hiddenFooterView:NO];
    
//    [self initWithTopView];
    self.flowLayout.itemSize = CGSizeMake(kRealValue(166), kRealValue(240));
    self.flowLayout.minimumLineSpacing = kRealValue(12); // 设置最小行间距
    self.flowLayout.minimumInteritemSpacing = kRealValue(12); // 设置垂直间距
//    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
    self.flowLayout.sectionInset = UIEdgeInsetsMake(kRealValue(0), kRealValue(15), kRealValue(20), kRealValue(15));
    self.baseCollectionView.backgroundColor = kWhiteColor;
    self.baseCollectionView.top = 0;//self.topView.bottom;
    self.baseCollectionView.height = kScreenH - NAVBAR_HEIGHT_STATUS - TABBAR_HEIGHT;
    [self.baseCollectionView registerClass:[AEPointsMallTopView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"AEPointsMallTopView"];
    [self.baseCollectionView registerClass:[AEPointsMallViewCell class] forCellWithReuseIdentifier:[AEPointsMallViewCell className]];
    
    
//    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
}

//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleDefault;
//}

- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kAEPointsMallTopViewHeight);
        _topView = [[AEPointsMallTopView alloc] initWithFrame:frame];
        [self.view addSubview:_topView];
        
        @weakify(self);
        // 兑换商品
        _topView.touchExchangeGoodsBlock = ^{
            @strongify(self);
            [self actionWithExchangeGoods];
        };
        
        // 历史积分
        _topView.touchHistoryPointsBlock = ^{
            @strongify(self);
            [self actionWithHistoryPoints];
        };
        
        // 我的兑换
        _topView.touchMyExchangeBlock = ^{
            @strongify(self);
            [self actionWithMyExchange];
        };
        
        // 获取更多积分
        _topView.touchMorePointsBlock = ^{
            @strongify(self);
            [self actionWithMorePoints];
        };
    }
    [self.topView updateWithViewData:self.viewModel];
}

/// 兑换商品
- (void)actionWithExchangeGoods {
    [SVProgressHUD showInfoWithStatus:@"兑换商品"];
}

/// 历史积分
- (void)actionWithHistoryPoints {
//    [SVProgressHUD showInfoWithStatus:@"历史积分"];
    AEHistoryPointsViewController *pointsVC = [[AEHistoryPointsViewController alloc] init];
    [TYZJumpVCManager pushBaseVC:self pushVC:pointsVC];
}

/// 我的兑换
- (void)actionWithMyExchange {
//    [SVProgressHUD showInfoWithStatus:@"我的兑换"];
    NSArray *subVCs = [AEMyExchangeRootViewController setupViewControllers:@{}];
    NSArray *titles = [AEMyExchangeRootViewController setupMenuTitles];
    TYZPageConfigration *config = [AEMyExchangeRootViewController setupConfig];
    AEMyExchangeRootViewController *vc = [[AEMyExchangeRootViewController alloc] initPageWithControllers:subVCs titles:titles config:config];
//    AEMyExchangeViewController *subVC = [[AEMyExchangeViewController alloc] init];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 获取更多积分
- (void)actionWithMorePoints {
//    [SVProgressHUD showInfoWithStatus:@"获取更多积分"];
    AEPointsAcquireViewController *vc = [[AEPointsAcquireViewController alloc] init];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.viewModel.dataList.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        AEPointsMallTopView *headerV = (AEPointsMallTopView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"AEPointsMallTopView" forIndexPath:indexPath];
        
        @weakify(self);
        // 兑换商品
        headerV.touchExchangeGoodsBlock = ^{
            @strongify(self);
            [self actionWithExchangeGoods];
        };
        
        // 历史积分
        headerV.touchHistoryPointsBlock = ^{
            @strongify(self);
            [self actionWithHistoryPoints];
        };
        
        // 我的兑换
        headerV.touchMyExchangeBlock = ^{
            @strongify(self);
            [self actionWithMyExchange];
        };
        
        // 获取更多积分
        headerV.touchMorePointsBlock = ^{
            @strongify(self);
            [self actionWithMorePoints];
        };
        
        self.topView = headerV;
        reusableview = headerV;
    }
    if (kind == UICollectionElementKindSectionFooter) {
        
    }
    return reusableview;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //        /*WDCarsOfSeriesModel*/ *model = self.viewModel.recomVehicleModels[indexPath.row];
    AEPointsMallViewCell *cell =[AEPointsMallViewCell cellForCollectionView:collectionView forIndexPath:indexPath];
    cell.contentView.size = self.viewModel.itemSize;
    [cell updateViewCell:nil];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    AEGoodsDetailViewController *goodsDetailVC = [[AEGoodsDetailViewController alloc] init];
    [TYZJumpVCManager pushBaseVC:self pushVC:goodsDetailVC];
}

//#pragma mark 定义每个UICollectionViewCell 的大小
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (indexPath.section == 0) {
//        return CGSizeMake(kScreenW, kWDRecomVehicleModelCellHeight);
//    }
//    float width = (kScreenW - kRealValue(18) * 2 - kRealValue(14)) * 0.5;
//    return CGSizeMake(width, kRealValue(210));
//}

// 设置头的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size = CGSizeZero;
    size = CGSizeMake(kScreenW, kAEPointsMallTopViewHeight);
    return size;
}

// 设置footer大小
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    CGSize size = CGSizeZero;
//    return size;
//}

//#pragma mark 定义每个分组的外边距大小
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    if (section == 0) {
////        return UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);
//        return UIEdgeInsetsMake(0, 0, 0, 0);
//    }
//    return UIEdgeInsetsMake(kRealValue(8), kRealValue(16), kRealValue(0), kRealValue(16));
//}

//#pragma mark  定义每个UICollectionView的横向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//    if (section == 0) {
//        return 0;
//    }
//    return kRealValue(12);
//}

//#pragma mark  定义每个UICollectionView的纵向间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//    if (section == 0) {
//        return 0;
//    }
//    return kRealValue(12);
//}

//override func navigationBarStyle() -> NavigationBarStyle {
//    return .baseColor
//}


@end
